home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / SocketWriter.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  1.3 KB  |  38 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.FilterOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6.  
  7. class SocketWriter extends FilterOutputStream {
  8.    byte[] _packetHeader = new byte[5];
  9.  
  10.    SocketWriter(OutputStream out) {
  11.       super(out);
  12.       this._packetHeader[0] = -69;
  13.    }
  14.  
  15.    public synchronized void write(short ID, int b) throws IOException {
  16.       byte[] i = new byte[1];
  17.       i[0] = (byte)b;
  18.       this.write(ID, i, 0, 1);
  19.    }
  20.  
  21.    public synchronized void write(short ID, byte[] b) throws IOException {
  22.       this.write(ID, b, 0, b.length);
  23.    }
  24.  
  25.    public synchronized void write(short ID, byte[] b, int off, int len) throws IOException {
  26.       int bufsize = len + 2;
  27.       this._packetHeader[1] = (byte)(bufsize >> 8);
  28.       this._packetHeader[2] = (byte)(bufsize & 255);
  29.       this._packetHeader[3] = (byte)(ID >> 8);
  30.       this._packetHeader[4] = (byte)(ID & 255);
  31.       bufsize = len + 5;
  32.       byte[] msgbufSend = new byte[bufsize];
  33.       System.arraycopy(this._packetHeader, 0, msgbufSend, 0, 5);
  34.       System.arraycopy(b, 0, msgbufSend, 5, len);
  35.       super.out.write(msgbufSend, 0, bufsize);
  36.    }
  37. }
  38.